<?php

/**
 * @author sanchikaro
 *
 */
class Application_Model_util_MyImageUtil {
	public static function _findexts($filename)
    {
        $filename = strtolower($filename) ;
        		
        		$exts = split("[/\.]", $filename) ;
        		
        		$n = count($exts)-1;
        		
        		$exts = $exts[$n];
        		
        		return $exts;
    }
    
    /**
     * phương thức lưu file submit trong request
     *
     * 
     *
     * @return một mảng tên file đã upload
     */
    public static function makeUpload($path,$request,$maxHeight,$maxWidth){
    	$name=array();
    	if($request->isPost()) {
        			$adapter=new Zend_File_Transfer_Adapter_Http();
        			$uploadDir= realpath(APPLICATION_PATH . '/../public/images/'.$path) ;
        			$adapter->setDestination($uploadDir);
        			$adapter->addValidator('FilesSize', false, array('min' => '0kB', 'max' => '1MB'));
        			$adapter->addValidator('Extension', false, array('jpg','JPG', 'png'));
        			
        			$i= 1;
        				foreach ($adapter->getFileInfo() as $info) {
        				$ext=Application_Model_util_MyImageUtil::_findexts($info['name']);
        				$fileName = 'file'.$i.time().".".$ext;
        				
        				$adapter->addFilter('Rename',
        				array('target' => $uploadDir.DIRECTORY_SEPARATOR.$fileName,
        				'overwrite' => true));
        					if(!$adapter->receive($info['name'])){
        					return $name;
        					}
        				
        				//resize anh
        				$file=$uploadDir.'/'.$fileName;
        				$imgSize=getimagesize($file);
        				$heightOld=$imgSize[1];
        				$widthOld= $imgSize[0];
        				$type=$imgSize['mime'];
        				
        				$ratio_orig =$widthOld/$heightOld;
        				
        				$fileOut=$uploadDir.'/rz_'.$fileName;
        				
        				if($maxWidth/$maxHeight>$ratio_orig){
        					$maxWidth=$maxHeight*$ratio_orig;
        				}else{
        					$maxHeight=$maxWidth/$ratio_orig;
        				}
        				if($heightOld<=$maxHeight&&$widthOld<=$maxWidth){
        					$maxHeight=$heightOld;
        					$maxWidth=$widthOld;
        				}
        				$thumb = imagecreatetruecolor($maxWidth, $maxHeight);
        				
        				
        				if($type=='image/jpeg'){
        					$source = imagecreatefromjpeg($file);
        					imagecopyresampled($thumb,$source,0,0,0,0,$maxWidth,$maxHeight,$widthOld,$heightOld);
        					imagejpeg($thumb,$fileOut);
        				}elseif ($type=='image/png'){
        					$black = imagecolorallocate($thumb, 0, 0, 0);
        					imagealphablending($thumb, false);
        					imagesavealpha($thumb,true);
							$transparent = imagecolorallocatealpha($thumb, 255, 255, 255, 127);
        					imagefilledrectangle($thumb, 0, 0, $maxWidth, $maxHeight, $transparent);
        					$source = imagecreatefrompng($file);
        					imagecopyresampled($thumb,$source,0,0,0,0,$maxWidth,$maxHeight,$widthOld,$heightOld);
        					imagepng($thumb,$fileOut);
        				}
        				
        				//xoa file cũ
        				unlink($file);
        				$name[$i-1]='rz_'.$fileName;
        				$i++;
        				}
            	}
     return $name;
    }
}

?>